package com.myblog.services.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myblog.Utils.BeanCopyUtils;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.LinkVo;
import com.myblog.domain.Vo.PageVo;
import com.myblog.domain.contants.GlobalConstants;
import com.myblog.domain.entity.Link;
import com.myblog.services.LinkService;
import com.myblog.mapper.LinkMapper;
import io.jsonwebtoken.lang.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【sg_link(友链)】的数据库操作Service实现
* @createDate 2022-09-26 21:40:57
*/
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper,Link> implements LinkService{

    @Override
    public GlobalResponse getAllLink() {
        //得是已经审核通过的
        LambdaQueryWrapper<Link> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, GlobalConstants.LINK_STATUS_NORMAL);
        List<Link> LinkList = list(queryWrapper);
        //Bean拷贝
        List<LinkVo> linkVoList = BeanCopyUtils.copyBeanList(LinkList, LinkVo.class);

        return GlobalResponse.ok(linkVoList);
    }

    @Override
    public GlobalResponse getLinkList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.hasText(name),Link::getName,name);
        lambdaQueryWrapper.eq(Strings.hasText(status),Link::getStatus,status);
        Page<Link> page=new Page<>();
        page(page,lambdaQueryWrapper);

        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(page.getRecords(), LinkVo.class);

        return GlobalResponse.ok(new PageVo(linkVos,page.getTotal()));

    }
}
